//
//  NCViewController.m
//  NCChatUI
//
//  Created by LINAICAI on 04/09/2018.
//  Copyright (c) 2018 LINAICAI. All rights reserved.
//

#import "NCViewController.h"
#import "TIMChatViewController.h"
#import <NCChatUI/NCChatUserInfoManager.h>
@interface NCViewController ()<NCChatUserInfoDataSource>

@end

@implementation NCViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationController.navigationBar setTranslucent:NO];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"聊天" style:UIBarButtonItemStyleDone target:self action:@selector(chat)];
    
    [NCChatUserInfoManager shared].delegate = self;
	// Do any additional setup after loading the view, typically from a nib.
}
- (void)getUserWith:(NSString *)senderId completion:(void (^)(NCChatUser *))completion{
    ///如果缓存有,直接返回缓存的用户信息
    NCChatUser *cache = [NCChatUserInfoManager.shared getUserCache:senderId];
    if (cache) {
        completion(cache);
        return;
    }
    ///没有缓存,从远程服务器获取
    if ([senderId isEqualToString:@"linaicai"]) {
        NSLog(@"从服务器获取用户数据");
        NCChatUser *user = [[NCChatUser alloc]initUserName:@"李柰财" userId:senderId userAvator:@"https://cdn.v2ex.com/gravatar/48e37833ce9eaf9500458f08a16e6013?s=48&d=retro"];
        ///获取到新的用户数据要更新到缓存里面去
        [NCChatUserInfoManager.shared refreshUserInfoCache:user];
        completion(user);
        
    }
    else{
        NSLog(@"从服务器获取用户数据");
        NCChatUser *user = [[NCChatUser alloc]initUserName:@"其他人" userId:senderId userAvator:@"https://cdn.v2ex.com/avatar/870f/1ee3/235833_normal.png?m=1502332729"];
        ///获取到新的用户数据要更新到缓存里面去
        [NCChatUserInfoManager.shared refreshUserInfoCache:user];
        completion(user);
    }

}
- (void)chat{
    TIMChatViewController *vc = [[TIMChatViewController alloc]initWithUserId:@"000000"];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
